package com.lagou.service.impl;

import com.lagou.dao.UserDao;
import com.lagou.pojo.Token;
import com.lagou.pojo.User;
import com.lagou.service.UserService;
import com.lagou.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;

/**
 * @author zy
 * @version 1.0
 * @date 2021/3/16 14:51
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Integer registered(String email, String password, String code) {
        try {
            // 判断该邮箱是否注册
            if ( checkEmail(email).isStatus() ){
                return 3;
            }else {
                // 检验验证码是否正确
                // 发送邮件
                String url = "http://lagou-service-code/api/code/validate";
                MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
                paramMap.add("code",code);
                String status = restTemplate.postForObject(url, paramMap, String.class);
                if ( status.equals("1") ){
                    // 验证码正确，生成令牌，返回成功
                    saveToken(email, password);
                    return 0;
                }else if (status.equals("0")){
                    return 1;
                }else if (status.equals("2")){
                    return 2;
                }else {
                    return 1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    @Override
    public User checkEmail(String email) {
        Optional one = getOptional(email);
        if ( one.isPresent() ){
            Token o = (Token) one.get();
            User user = new User(true, o.getToken());
            return user;
        }else {
            User user = new User(false,null);
            return user;
        }
    }

    public Optional<Token> getOptional(String email){
        Token token = new Token();
        token.setEmail(email);
        Optional<Token> one = userDao.findOne(Example.of(token));
        return one;
    }

    @Override
    public boolean saveToken(String email, String password) {
        try {
            String token = JwtUtil.sign(email, password);
            Optional optional = getOptional(email);
            Token token1 = new Token();
            token1.setEmail(email);
            if ( optional.isPresent() ){
                token1 = (Token) optional.get();
                token1.setToken(token);
            }else {
                token1.setId((long) 0);
                token1.setToken(token);
            }
            userDao.save(token1);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String getEmailByToken(String token) {
        return JwtUtil.getUsername(token);
    }
}
